from django.db import models
from django.contrib.auth.models import User
import datetime

class Garden(models.Model):
	name = models.CharField(max_length=200)
	user = models.ForeignKey(User)
	data = models.TextField()
	created = models.DateTimeField(auto_now=False, auto_now_add=True, editable=False)
	updated = models.DateTimeField(auto_now=True, auto_now_add=False, editable=False)

	user_water = models.PositiveIntegerField(null=True)
	user_waste = models.PositiveIntegerField(null=True)
	user_emiss = models.PositiveIntegerField(null=True)
	
	conv_water = models.PositiveIntegerField(null=True)
	conv_waste = models.PositiveIntegerField(null=True)
	conv_emiss = models.PositiveIntegerField(null=True)
	
	area_total = models.PositiveIntegerField(null=True)
	qualified  = models.BooleanField()
	
	def save(self, *args, **kwargs):
		if not self.id:
			self.created = datetime.datetime.today()
		self.updated = datetime.datetime.today()
		super(Garden, self).save()
		
	def __unicode__(self):
		return u'%s (%s)' %(self.name, self.user.username)

#Read-only table to be updated by site administrator only, containing data on individual plant species
#drawn from the WUCOLS database.
class Plant(models.Model):
	variety = models.CharField(max_length=12)
	latin  = models.CharField(max_length=64)
	common = models.CharField(max_length=64)
	water = models.CharField(max_length=2)
	kc = models.DecimalField(max_digits=2, decimal_places=1)
	invasive = models.BooleanField()
	
	def __unicode__(self):
		return u'%s (%s)' %(self.common, self.latin)

# This table supports the photos table. It contains the full information on the different types 
# of licenses that the photos have attached to them (e.g. Creative Commons Attribution 2.0, etc.)
class License(models.Model):
	short_name = models.CharField(max_length=24)
	long_name = models.CharField(max_length=72)
	link = models.URLField()

# The photos table contains all the information about the image files that we have stored
# As of now, it is incomplete, and not every plant has a related image.
class Photo(models.Model):
	img = models.CharField(max_length=128)
	# There is a one-to-one relationship between the tables Plants and Photos
	plant = models.OneToOneField(Plant)
	
	creator = models.CharField(max_length=55)
	year = models.PositiveIntegerField()
	
	# This is a many-to-one relationship (i.e. many photos can share a CC-BY-NA-2.0 license)
	license = models.ForeignKey(License)
	link = models.URLField()
	width = models.PositiveIntegerField()
	height = models.PositiveIntegerField()

